//store 全局变量参数 使用vuex管理
define(function (require, exports, module) {
    const Vue=require("vue");
    const Vuex=require("Vuex");
    const api=require('api');
    const sdk=require('sdk');
    const utils=require('utils');
    //用户信息存储key
    const storage_key="USERBIKE_USER_INFO";
    //oauth信息存储key
    const oauth_user_storage_key="OAUTH_USERBIKE_USER_INFO";
    const storeConfig= {
        state: {
            userInfo: window.localStorage.getItem(storage_key) ? JSON.parse(window.localStorage.getItem(storage_key)) : {},
            history: [],//历史记录
            isBack: false,//页面回退
            sdkInit: false,//sdk初始化完成
            oauth: window.localStorage.getItem(oauth_user_storage_key) ? JSON.parse(window.localStorage.getItem(oauth_user_storage_key)) : {},//
        },
        getters: {
            getUserInfo: function (state) {
                return state.userInfo;
            },
            getHistory: function (state) {
                return state.history;
            },
            getIsBack: function (state) {
                return state.isBack;
            },
            getSdkInit: function (state) {
                return state.sdkInit;
            },
            getOAuth: function (state) {
                return state.oauth;
            }
        },
        //同步方法，可直接修改状态
        mutations: {
            SET_USER: function (state, playload) {
                delete playload.return_code;
                delete playload.return_msg;
                state.userInfo = playload;
                window.localStorage.setItem(storage_key, JSON.stringify(playload));
            },
            DELETE_USER: function (state) {
                state.userInfo = {};
                window.localStorage.removeItem(storage_key);
            },
            //新增历史记录
            addHistory: function (state, payload) {
                state.history.push(payload);
                state.isBack = false;
            },
            //回退历史记录
            backHistory: function (state) {
                if (state.history.length > 0) {
                    state.history.pop();
                    state.isBack = true;
                }
            },
            setSdkInit: function (state, payload) {
                if (!payload) payload = false;
                state.sdkInit = payload;
            },
            setOAuth: function (state, payload) {
                state.oauth = payload;
                window.localStorage.setItem(oauth_user_storage_key, JSON.stringify(payload));
            },
            //退出登录
            logOut: function (state) {
                window.localStorage.removeItem(storage_key);
                //window.localStorage.removeItem(oauth_user_storage_key);
                state.userInfo = {};
                //state.history = [];
                //state.isBack = false;
                //state.oauth = {};
            }
        },
        //可包含异步方法 提交同步方法修改状态，不能直接修改状态
        actions: {
            // 获取用户信息
            UserInfo: function (dispatch, playload) {
                return new Promise(function (resolve, reject) {
                    api.getUserInfo(playload).then(function (data) {
                        if(data.user_token_invalid){
                            dispatch.commit("logOut");
                            resolve(data);
                        }else {
                            const user_login = data.user_get_info;
                            if (user_login && user_login.return_code === "SUCCESS") {
                                user_login.token = playload.token;
                                dispatch.commit('SET_USER', user_login);
                                resolve(user_login);
                            } else {
                                reject(data);
                            }
                        }
                    }).catch(function (error) {
                        reject(error);
                    });
                });
            },
            SdkInit: function (dispatch) {
                return new Promise(function (resolve, reject) {
                    var url = window.location.href.split("#")[0];
                    url = encodeURIComponent(url);
                    api.getJsConfig(url).then(function (res) {
                        if (res.return_code === 'SUCCESS') {
                            sdk.init({
                                appid: res.appId,
                                timestamp: res.timestamp,
                                nonceStr: res.nonceStr,
                                signature: res.signature
                            }).then(function () {
                                dispatch.commit('setSdkInit', true);
                                resolve();
                            }).catch(function (e) {
                                reject(e);
                            })
                        } else {
                            reject(res);
                        }
                    }).catch(function (e) {
                        reject(e);
                    });
                });
            },
            // 授权用户信息
            oauthInfo: function (dispatch, playload) {
                return new Promise(function (resolve, reject) {
                    api.authCode(playload.code, playload.state, playload.openid).then(function (res) {
                        if (res.return_code === 'SUCCESS') {
                            if (res.open_id) {
                                dispatch.commit('setOAuth', res);
                            } else {
                                //window.location.href=utils.oauthUrl();
                            }
                            resolve(res);
                        } else {
                            reject(res);
                        }
                    }).catch(function (error) {
                        reject(error);
                    });
                });
            },
        }
    };
    //安装vuex模块
    Vue.use(Vuex);
    const store = new Vuex.Store(storeConfig);
    module.exports=store;
});
